package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasListGroupEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.ListGroupEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderFlowNodeModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/4/26 10:54
 * @since v1.0
 */
@Component
@Slf4j
public class WorkOrderListGroupHelp {
    @Resource
    private WorkOrderFlowNodeModel workOrderFlowNodeModel;
    @Resource
    private UserModel userModel;

    public PaasListGroupEntity getDefaultGroup(FormDataListDTO formDataListDTO) throws XbbException{
        PaasListGroupEntity paasListGroupEntity = new PaasListGroupEntity();
        ListGroupEnum listGroupEnum = ListGroupEnum.getBusinessGroup(formDataListDTO.getBusinessType(), formDataListDTO.getListGroupId().intValue());
        if (listGroupEnum == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.LIST_GROUP_NOT_EXIST);
        }
        if (Objects.equals(ListGroupEnum.UNKONW, listGroupEnum)) {
            return paasListGroupEntity;
        }
        try {
            Method method = this.getClass().getDeclaredMethod(listGroupEnum.getAlias(), FormDataListDTO.class, PaasListGroupEntity.class);
            method.setAccessible(true);
            method.invoke(this, formDataListDTO, paasListGroupEntity);
        } catch (Exception e) {
            log.error("WorkOrderListGroupHelp.getDefaultGroup，alias="+listGroupEnum.getAlias(), e);
            e.printStackTrace();
        }
        return paasListGroupEntity;
    }

    /**
     * 我创建的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void create(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.CREATORID.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.CREATORID.getType());
        conditionsEntityExt.setName(FieldTypeEnum.CREATORID.getDescription());
        conditionsEntityExt.setSubAttr("");
        conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(formDataListDTO.getUserId()));
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }

    /**
     * 我负责的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void responsible(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.OWNERID.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.OWNERID.getType());
        conditionsEntityExt.setName(FieldTypeEnum.OWNERID.getDescription());
        conditionsEntityExt.setSubAttr("");
        conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(formDataListDTO.getUserId()));
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }

    /**
     * 我协同的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void cooperate(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.COUSERID.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.COUSERID.getType());
        conditionsEntityExt.setName(FieldTypeEnum.COUSERID.getDescription());
        conditionsEntityExt.setSubAttr("");
        conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(formDataListDTO.getUserId()));
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }

    /**
     * 我抄送的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void copy(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.COPY_USER.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.COPY_USER.getType());
        conditionsEntityExt.setName(FieldTypeEnum.COPY_USER.getDescription());
        conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(formDataListDTO.getUserId()));
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }


    /**
     * 下属创建的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void createBySub(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(formDataListDTO.getLoginUser(),userEntity);
        Integer dataPermission = userModel.getDataPermission(userEntity, SaasMarkEnum.SAAS, XbbRefTypeEnum.WORK_ORDER.getCode(), null);
        List<String> userIdIn = userModel.getSubIdListByDataPermission(dataPermission, userEntity.getUserId(), userEntity.getCorpid(), BasicConstant.ONE,true);
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.CREATORID.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.CREATORID.getType());
        conditionsEntityExt.setName(FieldTypeEnum.CREATORID.getDescription());
        conditionsEntityExt.setSubAttr("");
        conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        if (userIdIn.isEmpty()) {
            conditionsEntityExt.setValue(Collections.singletonList(-1));
        } else {
            conditionsEntityExt.setValue(new ArrayList<>(userIdIn));
        }
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }

    /**
     * 下属负责的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void responsibleBySub(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(formDataListDTO.getLoginUser(),userEntity);
        Integer dataPermission = userModel.getDataPermission(userEntity, SaasMarkEnum.SAAS, XbbRefTypeEnum.WORK_ORDER.getCode(), null);
        List<String> userIdIn = userModel.getSubIdListByDataPermission(dataPermission, userEntity.getUserId(), userEntity.getCorpid(),BasicConstant.ONE,true);
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt owerConditions = new ConditionsEntityExt();
        owerConditions.setAttr(FieldTypeEnum.OWNERID.getAlias());
        owerConditions.setFieldType(FieldTypeEnum.OWNERID.getType());
        owerConditions.setName(FieldTypeEnum.OWNERID.getDescription());
        owerConditions.setSubAttr("");
        owerConditions.setSymbol(ConditionEnum.IN.getSymbol());
        owerConditions.setValue(new ArrayList<>(userIdIn));
        conditionsEntityExtList.add(owerConditions);

        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }

    /**
     * 下属协同的
     *
     * @param formDataListDTO     前端参数
     * @param paasListGroupEntity 默认分组
     */
    @SuppressWarnings("unused")
    private void cooperateBySub(FormDataListDTO formDataListDTO, PaasListGroupEntity paasListGroupEntity) {
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(formDataListDTO.getLoginUser(),userEntity);
        Integer dataPermission = userModel.getDataPermission(userEntity, SaasMarkEnum.SAAS, XbbRefTypeEnum.WORK_ORDER.getCode(), null);
        List<String> userIdIn = userModel.getSubIdListByDataPermission(dataPermission, userEntity.getUserId(), userEntity.getCorpid(), BasicConstant.ONE,true);
        List<ConditionsEntityExt> conditionsEntityExtList = new ArrayList<>();
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(FieldTypeEnum.COUSERID.getAlias());
        conditionsEntityExt.setFieldType(FieldTypeEnum.COUSERID.getType());
        conditionsEntityExt.setName(FieldTypeEnum.COUSERID.getDescription());
        conditionsEntityExt.setSubAttr("");
        conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
        if (userIdIn.isEmpty()) {
            conditionsEntityExt.setValue(Collections.singletonList(-1));
        } else {
            conditionsEntityExt.setValue(new ArrayList<>(userIdIn));
        }
        conditionsEntityExtList.add(conditionsEntityExt);
        paasListGroupEntity.setJsonData(JSONArray.toJSONString(conditionsEntityExtList));
    }
}
